package com.relife.sys.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.relife.common.model.AjaxJson;
import com.relife.common.model.Page;
import com.relife.common.utils.StringUtil;
import com.relife.core.easyui.Easyui;
import com.relife.core.entity.DepartUserEntity;
import com.relife.core.entity.DepartmentEntity;
import com.relife.core.entity.ProcessConfigEntity;
import com.relife.core.entity.RoleEntity;
import com.relife.core.qbc.QbcQuery;
import com.relife.sys.service.ProcessConfigService;
import org.activiti.bpmn.model.UserTask;
import org.activiti.engine.RepositoryService;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.sql.JoinType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;



/**
 * 流程设置表
 * 
 * @author WU
 * @date 2016-05-19 17:17:20
 */
@Controller
@RequestMapping("sys/processConfig")
public class ProcessConfigController extends BaseController {
	@Autowired
	private ProcessConfigService pCService;
	@Autowired
	protected RepositoryService repositoryService;

	/**
	 * 
	 * @author WU
	 * @date 2016年5月20日 下午4:16:12
	 * @param req
	 * @return
	 */
	@RequestMapping
	public String processConfigList(HttpServletRequest req) {
		return "sys/processConfig/processConfigList";
	}

	/**
	 * 数据列表
	 * 
	 * @author WU
	 * @date 2016-05-19 17:17:20
	 * @param page
	 */
	@RequestMapping
	public void listData(ProcessConfigEntity pConfig, Page page, HttpServletResponse res) {
		QbcQuery qbc = new QbcQuery(pConfig);
		qbc.createAlias("depart", JoinType.INNER_JOIN);
		qbc.createAlias("processSet", JoinType.INNER_JOIN);
		pCService.getPage(qbc, page);
		Easyui.datagrid(page, res);
	}

	/**
	 * @author WU
	 * @date 2016-05-19 17:17:20
	 * @param req
	 * @return
	 */
	@RequestMapping
	public String processConfig(ProcessConfigEntity pConfig, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(pConfig.getId())) {
			pConfig = pCService.get(ProcessConfigEntity.class, pConfig.getId());
		}
		req.setAttribute("page", pConfig);
		return "sys/processConfig/processConfig";
	}

	/**
	 * 保存
	 * 
	 * @author WU
	 * @date 2016-05-19 17:17:20
	 * @return
	 */
	@ResponseBody
	@RequestMapping
	public AjaxJson save(ProcessConfigEntity pConfig) {
		AjaxJson j = new AjaxJson();
		try {
			pCService.saveProcessConfig(pConfig);
		} catch (Exception e) {
			j.setException(e);
			e.printStackTrace();
		}
		return j;
	}

	/**
	 * 删除
	 * 
	 * @author WU
	 * @date 2016-05-19 17:17:20
	 * @return
	 */
	@ResponseBody
	@RequestMapping
	public AjaxJson del(ProcessConfigEntity pConfig) {
		AjaxJson j = new AjaxJson();
		try {
			pConfig = service.get(ProcessConfigEntity.class, pConfig.getId());
			pCService.delete(pConfig);
		} catch (Exception e) {
			j.setException(e);
			e.printStackTrace();
		}
		return j;
	}

	/**
	 * 
	 * @author WU
	 * @date 2016年5月20日 下午4:45:19
	 * @param page
	 * @param res
	 */
	@RequestMapping
	public void departTree(Page page, HttpServletResponse res) {
		QbcQuery qbc = new QbcQuery(DepartmentEntity.class);
		qbc.addOrder(Order.asc("orderBy"));
		qbc.add(Restrictions.le("type", 3));
		this.openPermission(qbc);
		service.getPage(qbc, page);
		Easyui.ztree(page, res, true);
	}

	/**
	 * 
	 * @author WU
	 * @date 2016年5月23日 上午10:36:45
	 * @param id
	 * @param page
	 * @param res
	 */
	@RequestMapping
	public void getUserTask(String id, Page page, HttpServletResponse res) {
		org.activiti.bpmn.model.Process process = repositoryService.getBpmnModel(id).getMainProcess();
		List<UserTask> list = new ArrayList<UserTask>();
		for (Object obj : process.getFlowElements()) {
			if (obj instanceof UserTask) {
				list.add((UserTask) obj);
			}
		}
		page.setList(list);
		Easyui.datagrid(page, res);
	}

	/**
	 * 选择用户
	 * 
	 * @author WU
	 * @date 2016年5月24日 上午8:59:19
	 * @param req
	 * @return
	 */
	@RequestMapping
	public String selectUser(HttpServletRequest req) {
		return "sys/processConfig/selectUser";
	}

	@RequestMapping
	public void selectUserData(Page page, DepartUserEntity du, HttpServletResponse res) {
		QbcQuery qbc = new QbcQuery(du);
		service.getPage(qbc, page);
		Easyui.datagrid(page, res);
	}

	/**
	 * 选择角色
	 * 
	 * @author WU
	 * @date 2016年5月24日 上午8:59:38
	 * @param req
	 * @return
	 */
	@RequestMapping
	public String selectRole(HttpServletRequest req) {
		return "sys/processConfig/selectRole";
	}

	/**
	 * 
	 * @author WU
	 * @date 2016年11月11日 下午5:00:59
	 * @param role
	 * @param page
	 * @param res
	 * @param req
	 */
	@RequestMapping
	public void selectRoleData(RoleEntity role, Page page, HttpServletResponse res, HttpServletRequest req) {
		String dcode = req.getParameter("departCodes");
		QbcQuery qbc = new QbcQuery(role, req.getParameterMap());
		if (StringUtil.isNotEmpty(dcode)) {
			qbc.createAlias("depart");
			dcode = dcode.substring(0, dcode.indexOf("C") > 0 ? dcode.indexOf("C") + 3 : (dcode.indexOf("B") > 0 ? dcode.indexOf("B") + 3 : 3));
			qbc.add(Restrictions.like("depart.departCode", dcode + "%"));
		}
		this.openPermission(qbc);
		service.getPage(qbc, page);
		Easyui.datagrid(page, res);
	}

}
